package account

import (
	"context"
	"github.com/google/uuid"
	"github.com/spf13/cast"
	"insighttracking/common/utils"
	"insighttracking/query/dao"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetAccountInfoApiLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAccountInfoApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAccountInfoApiLogic {
	hostname, _ := os.Hostname()
	return &GetAccountInfoApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAccountInfoApiLogic) GetAccountInfoApi() (resp *types.AccountModel, err error) {
	accountId := l.ctx.Value("accountId")
	accountEntity, err := dao.AccountEntity.WithContext(l.ctx).Where(dao.AccountEntity.AccountID.Eq(cast.ToInt64(accountId))).First()
	if err != nil {
		return nil, err
	}
	return &types.AccountModel{
		AccountID:     accountEntity.AccountID,                              // 主键id
		Username:      accountEntity.Username,                               // 用户名
		AccountType:   accountEntity.AccountType,                            // 账号类型,0表示超管,1表示后台运营人员,1表示门店人员
		Status:        accountEntity.Status,                                 // 状态0表示正常,1表示禁用
		LastLoginDate: utils.TimestampToString(accountEntity.LastLoginDate), // 最后登录时间
		LastLoginIP:   accountEntity.LastLoginIP,                            // 最后登录ip地址
		CreatedAt:     utils.TimestampToString(accountEntity.CreatedAt),     // 创建时间
		UpdatedAt:     utils.TimestampToString(accountEntity.UpdatedAt),     // 更新时间
	}, nil
}
